package com.like.entity;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.like.likeutils.network.GsonUtil;
import com.like.likeutils.storage.SPUtils;
import com.like.utils.Constants;

import java.lang.reflect.Type;
import java.util.List;

public class M extends BaseM {

    public static final String FLAG_IS_FIRST_RUM = "is_first_run";

    public static final String TOKEN = "token";
    public static final String LATITUDE = "latitude";
    public static final String LONGITUDE = "longitude";
    public static final String LOGIN_ESP = "login_resp";
    public static final String ALL_DEPS = "all_deps";
    public static final String CURRENT_DEP = "current_dep";

    public M(Activity activity) {
        super(activity);
    }

    public M(Fragment fragment) {
        super(fragment);
    }

    public boolean isFirstRun() {
        return (boolean) SPUtils.get(mContext, FLAG_IS_FIRST_RUM, true);
    }

    public void setLoginResp(LoginResp resp) {
        String json = GsonUtil.gson.toJson(resp);
        SPUtils.put(mContext, LOGIN_ESP, json);
    }

    public LoginResp getLoginResp() {
        String json = (String) SPUtils.get(mContext, LOGIN_ESP, "");
        if (TextUtils.isEmpty(json))
            return null;
        return GsonUtil.gson.fromJson(json, LoginResp.class);
    }

    public void setAllDeps(List<Department> deps) {
        String json = GsonUtil.gson.toJson(deps);
        SPUtils.put(mContext, ALL_DEPS, json);
    }

    public List<Department> getAllDeps() {
        String json = (String) SPUtils.get(mContext, ALL_DEPS, "");
        if (TextUtils.isEmpty(json))
            return null;
        Type type = new TypeToken<List<Department>>() {
        }.getType();
        return GsonUtil.gson.fromJson(json, type);
    }

    public void setCurrentDep(Department dep) {
        String json = GsonUtil.gson.toJson(dep);
        SPUtils.put(mContext, CURRENT_DEP, json);
    }

    public Department getCurrentDep() {
        String json = (String) SPUtils.get(mContext, CURRENT_DEP, "");
        if (TextUtils.isEmpty(json))
            return null;
        return GsonUtil.gson.fromJson(json, Department.class);
    }

    public String getToken() {
        if (TextUtils.isEmpty(Constants.TOKEN))
            Constants.TOKEN = (String) SPUtils.get(mContext, TOKEN, "");
        return Constants.TOKEN;
    }

    public void setToken(String token) {
        Constants.TOKEN = token;
        SPUtils.put(mContext, TOKEN, token);
    }

    public void logout() {
        SPUtils.remove(mContext, LOGIN_ESP);
    }

    // 精度
    public void setLatitude(double latitude) {
        SPUtils.put(mContext, LATITUDE, latitude);
    }

    public String getLatitude() {
        return (String) SPUtils.get(mContext, LATITUDE, "0");
    }

    // 维度
    public void setLongitude(double longitude) {
        SPUtils.put(mContext, LONGITUDE, longitude);
    }

    public String getLongitude() {
        return (String) SPUtils.get(mContext, LONGITUDE, "0");
    }
}
